
/*
**
** Copyright (c) 2010 by Lorimark Solutions, LLC.
** All Rights Reserved.
** Send inquiries to: info@lorimarksolutions.com
**
*/

        .global  inl
        .global  inh
        .global  outl
        .global  outh
        .global  s16
        .global  s32

        .comm    inl,2,1                              ; in lo value
        .comm    inh,2,1                              ; in hi value
        .comm    outl,2,1                             ; out lo value
        .comm    outh,2,1                             ; out hi value
        .comm    s16,2,1                              ; 16-bit input/output
        .comm    s32,4,1                              ; 32-bit temp register

/*!
** \brief void shiftIn(void)
**
** This takes a 16-bit value and shifts it in to a 32-bit space,
**  and over 12 bits, for fixed-point precision.
**
*/
        .global  shiftIn
shiftIn:
        lds     r24, s16+0                            ; lower 16-bits come from s16 ram
        lds     r25, s16+1                            ;
        ldi     r26, 0x00                             ; upper 16-bits initialized to zero
        ldi     r27, 0x00                             ;
        ldi     r23, 12                               ; 12 bits shifted
si:     add     r24, r24                              ;
        adc     r25, r25                              ;
        adc     r26, r26                              ;
        adc     r27, r27                              ;
        dec     r23                                   ; shift until 12 bits are shifted
        brne    si                                    ;
                                                      ;
        sts     s32+0, r24                            ; store the result to the s32 temp
        sts     s32+1, r25                            ;
        sts     s32+2, r26                            ;
        sts     s32+3, r27                            ;
                                                      ;
        ret                                           ;

/*!
** \brief sint16 shiftOut( void )
**
** This takes a 32-bit value from the internal s32 register, and
**  shifts it down 12-bits and out to the s16 variable.
**
*/
        .global  shiftOut
shiftOut:
        ldi     r23, 12                               ; 12 bits shifted
        lds     r24, s32+0                            ; get the s32 value
        lds     r25, s32+1                            ;
        lds     r26, s32+2                            ;
        lds     r27, s32+3                            ;
so:     clc                                           ;
        ror     r27                                   ; shift down a byte at a time
        ror     r26                                   ;
        ror     r25                                   ;
        ror     r24                                   ;
        dec     r23                                   ;
        brne    so                                    ;
                                                      ;
        sts     s16+0, r24                            ; store the 16 bit value
        sts     s16+1, r25                            ;
                                                      ;
        ret                                           ; return the 16 bit value as well

/*!
** \brief void ConvertReading();
**
** This takes the values in the conversion memory set and processes them
**  as a conversion.
**
**
*/
        .global  ConvertReading

; 1d0:	80 91 56 02 	lds	r24, 0x0256
; 1d4:	90 91 57 02 	lds	r25, 0x0257
; 1d8:	08 1b       	sub	r16, r24
; 1da:	19 0b       	sbc	r17, r25
; 1dc:	10 93 61 02 	sts	0x0261, r17
; 1e0:	00 93 60 02 	sts	0x0260, r16

        rcall   shiftIn                               ; shift the 

ConvertReading:

        ret


